﻿namespace _17.ObserverPattern
{
    /*
    internal class Program
    {
        static void Main(string[] args)
        {
            // 实例化订阅者和订阅号对象
            Subscriber LearningHardSub = new Subscriber("LearningHard");
            TenxunGame txGame = new TenxunGame();

            txGame.Subscriber = LearningHardSub;
            txGame.Symbol = "TenXun Game";
            txGame.Info = "Have a new game published ....";

            txGame.Update();

            Console.ReadLine();
        }
    }

    // 腾讯游戏订阅号类
    public class TenxunGame 
    {
        public Subscriber Subscriber { get; set; }
        public string Symbol { get; set; }
        public string Info { get; set; }
        public void Update() 
        {
            if (Subscriber != null) 
            {
                // 调用订阅者对象来通知订阅者
                Subscriber.ReceiveAndPrintData(this);
            }
        }
    }

    // 订阅者类
    public class Subscriber
    {
        public string Name { get; set; }
        public Subscriber(string name) 
        {
            this.Name = name;
        }
        public void ReceiveAndPrintData(TenxunGame txGame) 
        {
            Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, txGame.Symbol, txGame.Info);
        }
    }
    */


    /*
    // 订阅号抽象类
    public abstract class TenXun 
    {
        // 保存订阅者列表
        private List<IObserver> observers = new List<IObserver>();

        public string Symbol { get; set; }
        public string Info { get; set; }
        public TenXun(string symbol, string info) 
        {
            this.Symbol = symbol;
            this.Info = info;
        }

        #region 增加对订阅号列表的维护操作
        public void AddObserver(IObserver ob) 
        {
            observers.Add(ob);
        }
        public void RemoveObserver(IObserver ob) 
        {
            observers.Remove(ob);
        }
        #endregion

        public void Update() 
        {
            // 遍历订阅者列表进行通知
            foreach (IObserver ob in observers) 
            {
                if (ob != null) 
                {
                    ob.ReceiveAndPrint(this);
                }
            }
        }
    }

    // 具挺订阅号类
    public class TenXunGame : TenXun 
    {
        public TenXunGame(string symbol, string info) 
            : base(symbol, info)
        {
            
        }
    }

    // 订阅者接口
    public interface IObserver 
    {
        void ReceiveAndPrint(TenXun tenxun);
    }

    public class Subscriber : IObserver
    {
        public string Name { get; set; }
        public Subscriber(string name)
        {
            this.Name = name;
        }
        public void ReceiveAndPrint(TenXun tenxun) 
        {
            Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tenxun.Symbol, tenxun.Info);
        }
    }

    class Program 
    {
        static void Main(string[] args) 
        {
            TenXun tenxun = new TenXunGame("TenXun Game", "Have a new game published ....");

            // 添加订阅者
            tenxun.AddObserver(new Subscriber("Learning Hard"));
            tenxun.AddObserver(new Subscriber("Tom"));

            tenxun.Update();

            Console.ReadLine();
        }
    }
    */



    class Program 
    {
        // 委托充当订阅者接口类
        public delegate void NotifyEventHandler(object sender);

        // 抽象订阅号类
        public class TenXun
        {
            public NotifyEventHandler NotifyEvent;

            public string Symbol { get; set; }
            public string Info { get; set; }
            public TenXun(string symbol, string info) 
            {
                this.Symbol = symbol;   
                this.Info = info;
            }

            #region 新增对订阅号列表的维护操作
            public void AddObserver(NotifyEventHandler ob)
            {
                NotifyEvent += ob;
            }
            public void RemoveObserver(NotifyEventHandler ob) 
            {
                NotifyEvent -= ob;
            }
            #endregion

            public void Update() 
            {
                if (NotifyEvent != null) 
                {
                    NotifyEvent(this);
                }
            }
        }

        // 具体订阅号类
        public class TenXunGame : TenXun 
        {
            public TenXunGame(string symbol, string info)
                : base(symbol, info)
            {
                
            }
        }

        // 具体订阅者类
        public class Subscriber 
        {
            public string Name { get; set; }
            public Subscriber(string name)
            {
                this.Name = name;
            }
            public void ReceiveAndPrint(object obj) 
            {
                TenXun tenxun = obj as TenXun;
                if (tenxun != null) 
                {
                    Console.WriteLine("Notified {0} of {1}'s" + "Info is: {2}", Name, tenxun.Symbol, tenxun.Info);
                }
            }
        }

        static void Main(string[] args) 
        {
            TenXun tenXun = new TenXunGame("TenXun Game", "Have a new game published ....");
            Subscriber lh = new Subscriber("Learning Hard");
            Subscriber tom = new Subscriber("Tom");

            // 添加订阅者
            tenXun.AddObserver(new NotifyEventHandler(lh.ReceiveAndPrint));
            tenXun.AddObserver(new NotifyEventHandler(tom.ReceiveAndPrint));

            tenXun.Update();

            Console.WriteLine("-------------------");
            Console.WriteLine("移除Tom订阅者");

            tenXun.RemoveObserver(new NotifyEventHandler(tom.ReceiveAndPrint));
            tenXun.Update();

            Console.ReadLine();
        }
    }

}
